package com.mzx.javase.yichang;

/**
 * @Description:throw和throws的区别
 * @Date: 2022/2/28 17:57
 * @Author: MarKing
 */
public class Test2 {
    /**
     * 1、throws，用于定义方法时，声明该方法向外抛出异常
     * 2、throw，主动抛出一个异常，打断程序的执行。配合try..catch，或者throws使用
     */
    public static void main(String[] args) {

    }

    public static int div(int x, int y) throws Exception {
        Exception myException = new Exception("这是我自定义的异常");
        if (y == 0) {
            //throw抛出异常时，配合throws向上抛出
            throw myException;
        }
        return x / y;
    }
    public static int div1(int x, int y) {
        Exception myException = new Exception("这是我自定义的异常");
        if (y == 0) {
            //throw抛出异常时，配合trycatch
            try {
                throw myException;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return x / y;
    }


}
